package dao

import (
	"fmt"
	"github.com/jinzhu/gorm"
	"go-admin/global"
	"go-admin/internal/model"
	mr "go-admin/internal/model/api/model_request"
	_const "go-admin/internal/model/const"
	"time"
)

func (d *Dao) CreateMenu(param *mr.CreateMenuRequest) (string, error) {
	menu := model.Menu{
		CommonModel: model.CommonModel{
			Id:    global.GetId(),
			IsDel: _const.ISDEL,
		},
		IsBanned:      _const.ISBANNED,
		Remark:        param.Remark,
		MenuTitle:     param.MenuTitle,
		ParentId:      param.ParentId,
		Icon:          param.Icon,
		Path:          param.Path,
		MenuType:      param.MenuType,
		Action:        param.Action,
		Permission:    param.Permission,
		ComponentPath: param.ComponentPath,
		IsIframe:      param.IsIframe,
		IsVisible:     param.IsVisible,
		RouteName:     param.RouteName,
	}
	// 获取当前父id的部门信息
	parentDept, err := menu.GetMenuById(d.Engine, menu.ParentId)
	if err != nil {
		return "", err
	}
	// 如果获取的id为0，说明没有找到当前行的数据,为顶级的数据
	if parentDept.Id == "" {
		menu.MenuPath = fmt.Sprintf("/0/%v", menu.Id)
	} else {
		menu.MenuPath = fmt.Sprintf("%v/%v", parentDept.MenuPath, menu.Id)
	}
	return menu.Id, menu.Create(d.Engine)
}

func (d *Dao) GetDeptByMenuTitle(param *mr.CreateMenuRequest) (*model.Menu, error) {
	menu := model.Menu{MenuTitle: param.MenuTitle}
	return menu.GetDeptByMenuTitle(d.Engine, menu.MenuTitle)
}

func (d *Dao) GetMenuListAll() ([]*model.Menu, error) {
	dept := model.Menu{}
	return dept.ListAll(d.Engine)
}

// 根据角色获取菜单
func (d *Dao) GetMenuListByRoleKey(roleKey []string) ([]*model.Menu, error) {
	dept := model.Menu{}
	return dept.ListByRoleKey(d.Engine, roleKey)
}

// 根据id获取详情
func (d *Dao) GetMenuById(id string) (*model.Menu, error) {
	menu := model.Menu{
		CommonModel: model.CommonModel{Id: id},
	}
	return menu.GetMenuById(d.Engine, id)
}

func (d *Dao) UpdateMenu(param *mr.UpdateMenuRequest) error {
	menu := model.Menu{
		CommonModel: model.CommonModel{
			Id: param.Id,
		},
	}
	values := make(map[string]interface{})
	values["modified_on"] = time.Now().Local().Unix()
	values["menuTitle"] = param.MenuTitle
	// 判断type类型
	switch param.MenuType {
	case "M": // 目录
		values["routeName"] = param.RouteName
		values["path"] = param.Path
		values["componentPath"] = param.ComponentPath
		values["isVisible"] = param.IsVisible
		values["isIframe"] = param.IsIframe
		break
	case "C": // 菜单
		values["routeName"] = param.RouteName
		values["path"] = param.Path
		values["componentPath"] = param.ComponentPath
		values["isVisible"] = param.IsVisible
		values["isIframe"] = param.IsIframe
		values["permission"] = param.Permission
		break
	case "F": // 按钮
		values["permission"] = param.Permission
		break
	case "A": // 接口
		values["action"] = param.Action
		values["path"] = param.Path
		values["isVisible"] = param.IsVisible
		break
	}
	if param.Remark != "" {
		values["remark"] = param.Remark
	}
	return menu.Update(d.Engine, values)
}

// 删除
func (d *Dao) DeleteMenu(id string) error {
	menu := model.Menu{
		CommonModel: model.CommonModel{
			Id: id,
		},
	}
	values := map[string]interface{}{
		"deleted_on": time.Now().Local().Unix(),
		"is_del":     _const.DEL,
	}
	return menu.Delete(d.Engine, values)
}

// 根据菜单ids获取指定类型的菜单
func (d *Dao) GetMenuInType(tx *gorm.DB, menuIds []string, menuType string) ([]*model.Menu, error) {
	menu := model.Menu{}
	return menu.GetMenuInType(tx, menuIds, menuType)
}
